Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance compatibility of emscripten built codecs with Cloudflare Workers #21

Merged
merged 10 commits into from
Jun 9, 2023

Conversation

jamsinclair
Copy link
Owner

Currently using the packages for avif, jpeg and webp would require workarounds at build time to work in Cloudflare Workers. This was shown in the Cloudflare Worker Example.

It required:

  • Polyfilling the ImageData class into the global scope
  • At build time replacing import.meta.url and self.location.href values.

This added the burden onto consumers of these packages and was highlighted in issues #20 and possibly #18

This PR:

  • Adds a pre script that polyfills these values in the Emscripten Module glue code at runtime – only when run in a Cloudflare Worker
  • Updates the examples to work with the latest module versions

@jamsinclair jamsinclair merged commit 84cfabd into main Jun 9, 2023
@jamsinclair jamsinclair deleted the better-cloudflare-worker-support branch June 9, 2023 14:29
@sengho66
Copy link

sengho66 commented Jul 1, 2023

When decoding or encoding using the png module, the object globalThis.ImageData will need to be polyfilled too. I don’t see the script pre.js being included in the module, I suppose this needs to be included in the source and also in Makefile?

Tested this on my machine by polyfilling it manually in my project, not sure how cpp works so I have no idea how to make a PR.

@jamsinclair
Copy link
Owner Author

Thanks for bringing that to my attentioin @sengho66, the png module is Rust based and so doesn't follow the usual emscripten approach.

I've created an issue (#25) and will follow up when I get a chance.

@jamsinclair
Copy link
Owner Author

@sengho66 I've released v2.1.0 of @jsquash/png, let me know if that works for you 🙇

@sengho66
Copy link

sengho66 commented Jul 8, 2023

@sengho66 I've released v2.1.0 of @jsquash/png, let me know if that works for you 🙇

Thank you, will have a look when I have the chance.

Also I am quite new to the open source scene, not sure if it’s alright to ask non-related question in a specific topic.

When importing the compiled binary of the webp’s wasm module directly, Typescript will warn you that module is not found because webp_enc_simd.wasm.d.ts doesn’t exist. I have written a script in Rust to emit the declaration by reading into the binary and then parse them and create a d.ts for it. However the names are mangled because of some of the clang flags tell the compiler to minify the emscripten’s code so the emitted declaration’s name will be weird. Do you happen to know how to have clang or emcc not to mangle and preserve the function name? Again, I do not know anything about cpp or emcc.

Thanks!

@jamsinclair
Copy link
Owner Author

When importing the compiled binary of the webp’s wasm module directly, Typescript will warn you that module is not found because webp_enc_simd.wasm.d.ts doesn’t exist.

You must be using some kind of bundler with your Frontend application, as normally you can't import wasm binaries directly. The easiest way to type it would be to cast it to WebAssembly.Module.

I have written a script in Rust to emit the declaration by reading into the binary and then parse them and create a d.ts for it.

This sounds very complicated 😅 . Would a simpler solution work? As you're likely using it with the generated glue code they're already typed as part of this library. See packages/webp/codec/enc/webp_enc.d.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants